﻿using Company.Application.Hardware.Services;
using Company.Application.Share.Prism;
using Company.Core.IOC;
using Prism.Commands;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Company.Application.Hardware.ViewModels
{
    public class HardwareInitViewModel : ReactiveObject
    {
        private readonly HardwareLifetimeManager _hardwareManager;
        public HardwareInitViewModel(HardwareLifetimeManager hardwareManager)
        {
            _hardwareManager = hardwareManager;
            LoadedCommand = new DelegateCommand(OnLoaded);
            UnloadedCommand = new DelegateCommand(OnUnloaded);
        }

        [Reactive]
        public string Message { get; set; }

        public ICommand LoadedCommand { get; }

        public ICommand UnloadedCommand { get; }
        
        private async void OnLoaded()
        {
            Message = "正在初始化硬件...";
            var result = await _hardwareManager.InitAsync();
            Message = result.Message;
            if (result.Success)
            {
                await Task.Delay(500); // 给硬件一点反应时间
                PrismProvider.Dispatcher.Invoke(() =>
                {
                    // 加载Main模块
                    PrismProvider.ModuleManager.LoadModule(ModuleNames.MainModule);
                    PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.MainView);
                    PrismProvider.MainWindow.WindowState = System.Windows.WindowState.Maximized;
                });
            }
        }

        private void OnUnloaded() { }
    }
}
